package com.sdp.lease.controller;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.lease.db.l_eq_lease_info.entity.LEqLeaseInfo;
import com.sdp.lease.db.l_eq_lease_info.service.ILEqLeaseInfoService;
import com.sdp.lease.utils.CurrentLoginEmp;
import com.sdp.lease.utils.LeaseConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-07-27
 */
@RestController
@RequestMapping("/LEqLeaseInfoController")
public class LEqLeaseInfoController {

	@Autowired
	private ILEqLeaseInfoService iLEqLeaseInfoService;
	
	@Autowired
	private Query query;
	
	@Autowired
	private CurrentLoginEmp currentLoginEmp;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iLEqLeaseInfoService, LEqLeaseInfo.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody LEqLeaseInfo eqLeaseInfo) {
		if(eqLeaseInfo==null) {
			return Result.error("设备求租信息为空");
		}
		if(StringUtils.isEmpty(eqLeaseInfo.getKeyId())) {
			eqLeaseInfo.setApplyState(LeaseConstants.申请状态_申请中.getCode());
			eqLeaseInfo.setApplyTime(LocalDateTime.now());
			iLEqLeaseInfoService.save(eqLeaseInfo);
		}else {
			iLEqLeaseInfoService.updateById(eqLeaseInfo);
		}
		return Result.ok();
	}
	
	/**
	 * 审批
	 * @param keyId
	 * @param applyState
	 * @return
	 */
	@GetMapping(value = "/approval")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> approval(@RequestParam(name="keyId",required=true) String keyId,@RequestParam(name="applyState",required=true) String applyState) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("求租信息不完整");
		}
		LEqLeaseInfo eqLeaseInfo = iLEqLeaseInfoService.getById(keyId);
		if(eqLeaseInfo==null) {
			return Result.error("求租信息不存在");
		}
		if(!LeaseConstants.申请状态_申请中.getCode().equals(eqLeaseInfo.getApplyState())) {
			return Result.error("此求租信息已经审批过");
		}
		//如果此申请未通过，这这个单子结束
		if(LeaseConstants.申请状态_申请未通过.getCode().equals(applyState)) {
			eqLeaseInfo.setIsHandle(LeaseConstants.是.getCode());
		}else {
			eqLeaseInfo.setIsHandle(LeaseConstants.否.getCode());
		}
		eqLeaseInfo.setApplyState(applyState);
		eqLeaseInfo.setApproverTime(LocalDateTime.now());
		eqLeaseInfo.setApproverNo(currentLoginEmp.getCurrentLoginInfo().getString("empNo"));
		eqLeaseInfo.setApproverName(currentLoginEmp.getCurrentLoginInfo().getString("empName"));
		iLEqLeaseInfoService.updateById(eqLeaseInfo);
		return Result.ok();
	}
	
	/**
	 * 办理
	 * @param keyId
	 * @return
	 */
	@GetMapping(value = "/handle")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> handle(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("求租信息不完整");
		}
		LEqLeaseInfo eqLeaseInfo = iLEqLeaseInfoService.getById(keyId);
		if(eqLeaseInfo==null) {
			return Result.error("求租信息不存在");
		}
		if(LeaseConstants.是.getCode().equals(eqLeaseInfo.getIsHandle())) {
			return Result.error("此求租信息已办理过");
		}
		eqLeaseInfo.setIsHandle(LeaseConstants.是.getCode());
		iLEqLeaseInfoService.updateById(eqLeaseInfo);
		return Result.ok();
	}
}

